package com.fbee.smartcommunities.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fbee.smartcommunities.entity.Goods;
import com.fbee.smartcommunities.entity.User;
import com.fbee.smartcommunities.service.GoodsService;
import com.fbee.smartcommunities.service.UserService;
import com.fbee.smartcommunities.vo.ResultVO;
import com.sun.org.apache.regexp.internal.RE;
import io.minio.errors.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author 汪佳泉
 * @since 2024-08-16
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    // 分页后内容
    @GetMapping("/list")
    public ResultVO<IPage<Goods>> getPage(Integer pageNo, Integer pageSize) {
        //return ResultVO.success(goodsService.page(new Page<>(pageNo,pageSize)),null);
        return ResultVO.success(goodsService.getPage(pageNo,pageSize));
    }

    @GetMapping("/getPage")
    public ResultVO<IPage<Goods>> getBusinessPage(Integer pageNo,
                                                  Integer pageSize,
                                                  Integer stateUser,
                                                  Long openId,
                                                  String sortType,
                                                  String sortDetail,
                                                  Integer typeId,
                                                  String goodsName){
        return ResultVO.success(goodsService.getBusinessPage(pageNo,pageSize,stateUser,openId,sortType,sortDetail,typeId,goodsName));
    }

    // 获取一个商品，用于表单回显
    @GetMapping("/getById")
    public ResultVO<Goods> getById(Long id){
        //return ResultVO.success(goodsService.getById(id));
        return ResultVO.success(goodsService.getById(id));
    }

    @GetMapping("/getByIdAll")
    public ResultVO<Goods> getByIdAll(Long id){
        return ResultVO.success(goodsService.getOne(new QueryWrapper<Goods>().eq("id",id)));
    }

    // 修改商品上架状态
    @PostMapping("/fixState")
    public ResultVO<Boolean> fixState(Long id, Boolean state,String key){
        return ResultVO.success(goodsService.fixState(id,state,key),"修改成功");
    }

    @GetMapping("/find")
    public ResultVO<IPage<Goods>> getTargetList(Integer pageNo, Integer pageSize, String keyword, Integer categoryId){
        return ResultVO.success(goodsService.getTargetList(pageNo,pageSize,keyword,categoryId));
    }

    @PostMapping("/recommend")
    public ResultVO<Boolean> fixRecommend(Long id, Boolean state,String key){
        return ResultVO.success(goodsService.fixState(id,state,key));
    }

    @GetMapping("/getRecommend")
    public ResultVO<List<Goods>> getRecommendGoods(){
        return ResultVO.success(goodsService.list(new QueryWrapper<Goods>().eq("recommend",1).eq("state",1).eq("state_user",1)));
    }

    @GetMapping("/getLowPrice")
    public ResultVO<List<Goods>> getLowPriceGoods(){
        return ResultVO.success(goodsService.list(new QueryWrapper<Goods>().eq("state",1).eq("state_user",1).ne("discount",0).orderByDesc("discount")));
    }

    @GetMapping("/listNoPage")
    public ResultVO<List<Goods>> getGoods(){
        return ResultVO.success(goodsService.list(new QueryWrapper<Goods>().eq("state",1).eq("state_user",1)));
    }

    @PostMapping("/increaseBrowse")
    public ResultVO<Boolean> increaseBrowse(Long id){
        return ResultVO.success(goodsService.increaseBrowse(id));
    }

    @PostMapping("/updateState")
    public ResultVO<Boolean> updateStateUser(Integer state,Long id){
        return ResultVO.success(goodsService.update(new UpdateWrapper<Goods>().eq("id",id).set("state_user",state)),"操作成功");
    }

    @PostMapping("/updateStateForBatch")
    public ResultVO<Boolean> updateStateUserForBatch(Integer[] ids,Long stateUser){
        return ResultVO.success(goodsService.updateStateUserForBatch(ids,stateUser),"修改成功");
    }

    @PostMapping("/del")
    ResultVO<Boolean> deleteById(Long id){
        return ResultVO.success(goodsService.removeById(id),"删除成功");
    }

    @PostMapping("/update")
    ResultVO<Boolean> updateGoods(Long id,
                                  String name,
                                  String description,
                                  Double discount,
                                  Double price,
                                  Integer store,
                                  MultipartFile file
                                  ) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return ResultVO.success(goodsService.updateGoods(id,name,description,discount,price,store,file),"商品修改成功");
    }

    @PostMapping("/add")
    ResultVO<Boolean> addGoods(Long categoryId,String name,Integer count,String description,Double price,MultipartFile file,Long openId) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return ResultVO.success(goodsService.addGoods(categoryId,name,count,description,price,file,openId),"添加商品成功");
    }

    @PostMapping("/getStore")
    ResultVO<List<Integer>> getGoodsStore(Long[] ids){
        return ResultVO.success(goodsService.getGoodsStore(ids));
    }

}
